package com.example.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.backend.entity.Role;
import com.example.backend.entity.User;
import com.example.backend.entity.UserRole;
import com.example.backend.mapper.RoleMapper;
import com.example.backend.mapper.UserMapper;
import com.example.backend.mapper.UserRoleMapper;
import com.example.backend.service.InitService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统初始化服务实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class InitServiceImpl implements InitService {
    
    private final UserMapper userMapper;
    private final RoleMapper roleMapper;
    private final UserRoleMapper userRoleMapper;
    private final PasswordEncoder passwordEncoder;
    
    @Value("${system.admin.username:admin}")
    private String adminUsername;
    
    @Value("${system.admin.password:admin123}")
    private String adminPassword;
    
    @Value("${system.admin.nickname:系统管理员}")
    private String adminNickname;
    
    @Value("${system.admin.email:admin@example.com}")
    private String adminEmail;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void init() {
        // 1. 初始化管理员账号
        initAdminUser();
        
        // 2. 其他初始化操作
        // ...
        
        log.info("系统初始化完成");
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void initAdminUser() {
        // 检查是否已存在管理员账号
        User admin = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, adminUsername)
                .last("LIMIT 1"));
        
        if (admin == null) {
            log.info("开始初始化管理员账号");
            
            // 1. 创建管理员用户
            admin = new User();
            admin.setUsername(adminUsername);
            admin.setPassword(passwordEncoder.encode(adminPassword));
            admin.setNickname(adminNickname);
            admin.setEmail(adminEmail);
            admin.setStatus(1);
            userMapper.insert(admin);
            
            // 2. 检查管理员角色是否存在
            Role adminRole = roleMapper.selectOne(new LambdaQueryWrapper<Role>()
                    .eq(Role::getCode, "ROLE_ADMIN")
                    .last("LIMIT 1"));
            
            if (adminRole == null) {
                // 创建管理员角色
                adminRole = new Role();
                adminRole.setName("管理员");
                adminRole.setCode("ROLE_ADMIN");
                adminRole.setStatus(1);
                roleMapper.insert(adminRole);
            }
            
            // 3. 分配管理员角色
            UserRole userRole = new UserRole();
            userRole.setUserId(admin.getId());
            userRole.setRoleId(adminRole.getId());
            userRoleMapper.insert(userRole);
            
            log.info("管理员账号初始化完成");
        }
    }
} 